package com.zhang.novel.core.schedule;

import com.zhang.novel.core.utils.Constants;
import com.zhang.novel.entity.Book;
import com.zhang.novel.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "pic.save", name = "type", havingValue = "2")
public class CrawlPicTransSchedule {

    private final BookService bookService;

    @SneakyThrows
    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void trans() {
        log.info("网络图片----》自己图片");

        List<Book> networkPicBooks = bookService.queryNetworkPicBooks(Constants.LOCAL_PIC_PREFIX, 100);

        for (Book networkPicBook : networkPicBooks) {
            bookService.updateBookPicToLocal(networkPicBook.getPicUrl(), networkPicBook.getId());

            Thread.sleep(3000);
        }
    }
}
